package com.zy.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.zy.dao.PermissionDao;
import com.zy.dao.RoleDao;
import com.zy.dao.UserDao;
import com.zy.pojo.Permission;
import com.zy.pojo.Role;
import com.zy.pojo.User;
import com.zy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;


@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {

	@Autowired
	private UserDao userDao;  //用户表
	@Autowired
	private RoleDao roleDao; //角色表
	@Autowired
	private PermissionDao permissionDao; //权限表


	//根据姓名  查询 User（包括里面的权限和角色） ----多对多关联查询
	@Override
	public User findUserByname(String username) {
		User user=userDao.findUserByname(username);
		if(user == null) {
			return null;
		}

		Set<Role> roles =roleDao.findByUserId(user.getId());

		if(roles!=null && roles.size() > 0){
			for (Role role : roles) {
				Set<Permission> permissions = permissionDao.findByRoleId(role.getId());
				if(permissions!=null && permissions.size() > 0){
					role.setPermissions(permissions);
				}
			}
			user.setRoles(roles);
		}
		return user;
	}
}
